const { $Toast } = require('../../../utils/iview/base/index')
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';


let app = getApp()

Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
    codeyzm: '',
    yzmConfig: {
      url: 'api/v1/member_phone_msn',
      method: 'POST',
      data: {
        phone: ''
      }
    
    },

    sex: 1,
    items: [{
      name: '男',
      value: 1,
      checked: true
    },
    {
      name: '女',
      value: 2
    },
    ],
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    user: {},
  },
 


  onLoad(options) {
    var _that = this
    _that.setTitle('个人资料');
    console.log(options.user)
    _that.users()
  
   
  },



  
  setCode(e) {

    console.log(e.detail.value);
    var value = e.detail.value;
    this.setData({
      codeyzm: value
    })
  },

  onShow: function () {
    var _that = this
    var user = wx.getStorageSync('user');
    _that.setData({
      isclick: 0,
      "yzmConfig.data.phone": user.phone
    })

  },

  phonee(e) {
    // console.log(e.detail.value,"values");
    this.setData({
      "yzmConfig.data.phone": e.detail.value
    })
  },



  users() {
    var _that = this;
    var token = wx.getStorageSync('token');
    if (!token) {
      return;
    }
    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/getMember',
      data: { token: token },
      method: 'POST',
    }, '', res => {
      if (res.data.error_code == 0) {
        _that.setData({
          user: res.data.data
        })
        // 这里设置用户缓存是为了给下单页面调取用户名称
        wx.setStorageSync("user", _that.data.user)
      }
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.users();
        }, 1500)
      }
    })
  },



  formSubmit(e) {
    var _that = this;

    var isclick = _that.data.isclick;

    if (isclick) {
      return;
    }

    _that.setData({
      isclick: 1
    })

    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 5000)


    var { value } = e.detail;
    _that.setData({
      dates: value
    })
   
    value.token = wx.getStorageSync('token');


    let reg_tpl = /^1[3456789]\d{9}$/
    for (let i in value) {
      let values = value[i];
      if (!values) {
        $Toast({
          content: '内容不能为空',
          type: 'none'
        })
        return;
      }
      if (i == 'phone' && !reg_tpl.test(values)) {
        $Toast({
          content: '手机号码不正确!',
          type: 'none'
        })
        return;
      }

    }

    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/check_member_code',
      method: 'POST',
      // data: value
      data: value
      //  {
      //   token:token,
      //   code: _that.data.codeyzm,
      //   phone: 13250594982
      // },

    }, '修改', res => {
      setTimeout(() => {
        wx.navigateBack({
          changed: true
        });
      }, 1000)
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          $Toast({
            content: "修改失败，请重新修改",
            type: 'warning'
          })
        }, 1500)
      }
      else if (res.data.error_code == 400) {
        $Toast({
          content: "修改失败",
          type: 'warning'
        })
      }

    })
  },



  scanCode: function () {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          scanCodeMsg: res.result
        });
        wx.showToast({
          title: '成功',
          duration: 1000
        })
      }
    })
  },



  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },


  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })

        }
      }
    })
  },


  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    console.log(_userInfo)
    wx.setStorageSync('_userInfo', _userInfo);
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true,
      })
    }


    wx.login({
      success(res) {
        if (res.code) {


          net._request({
            url: 'api/v1/login/mini',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, '', res => {
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);

            _that.setData({
              userInfo: user
            })

            wx.setStorageSync('user', {
              user_type: 1
            });
            $Toast({
              content: res.data.msg,
              type: 'success'
            });

          }, res => {
            console.log("失败返回", res);
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);
            $Toast({
              content: res.data.msg,
              type: 'success'
            });
          });
        }
      }
    })
  },


})